package com.mp.approvalreq.util;


import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Cc;
import com.mp.approvalreq.entity.Finished;
import com.mp.approvalreq.entity.Request;
import com.mp.user.entity.User;
import com.mp.user.service.UserServicePrx;
import com.plusmoney.util.Utils;
import com.qq.tars.client.util.ServantUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;

public class LookAuth {

    private static final Logger logger = LoggerFactory.getLogger(LookAuth.class);


    //老板、老板多身份、副总
    private static boolean userAuth(User user) {
        return user.getIsAdmin() == 1
                || user.getIsAdminIdentity() == 1
                || user.getIsAssistantManager() == 2;
    }


    /**
     * 判断当前用户是否可以查看该审批
     *
     * @param user
     * @param request
     * @return
     */
    public static boolean checkUserIsCanSeeRequest(User user, Request request) {
        boolean flag = false;

        int applicantCid = request.getApplicant();
        int applicantCiid = request.getApplicantCiid();
        String allJudgerCid = request.getAllJudger();
        String allJudgerCiid = request.getAllJudgerCiid();
        String relaPeopleCiid = request.getRelaPeopleCiid();

        int companyInfoId = user.getCompanyInfoId();
        String companyInfoIdStr = String.valueOf(companyInfoId);

        if (userAuth(user)) {
            return true;
        }

        // 申请人
        if (applicantCiid == companyInfoId) {
            return true;
        }
        // 代申请人
        if (Objects.equals(request.getProxyContactId(), user.getContactId())) {
            return true;
        }

        if (StringUtils.isNotEmpty(allJudgerCiid)) {
            // 审批人
            for (String allJudger : allJudgerCiid.split(",")) {
                if (Objects.equals(allJudger, companyInfoIdStr)) {
                    return true;
                }
            }
        }

        if (StringUtils.isNotEmpty(relaPeopleCiid)) {
            // 参与人
            for (String relaPeople : relaPeopleCiid.split(",")) {
                if (Objects.equals(relaPeople, companyInfoIdStr)) {
                    return true;
                }
            }
        }

        List<Cc> ccList = request.getCcList();
        if (request.getFinished() == Finished.AGREED || request.getFinished() == Finished.ARCHIVED) {
            if (Utils.isNotEmpty(ccList)) {
                if (ccList.stream().anyMatch(o -> Objects.equals(o.getCcCiid(), user.getCompanyInfoId()))) {
                    return true;
                }
            }
        }
        // 抄送人查看拒绝链上的审批
        if (Utils.isNotNull(request.getNewestRequest())) {
            logger.info("最新的审批:{}",JSONObject.toJSONString(request.getNewestRequest()));
            ccList = request.getNewestRequest().getCcList();
            if (request.getNewestRequest().getFinished() == Finished.AGREED || request.getNewestRequest().getFinished() == Finished.ARCHIVED) {
                if (Utils.isNotEmpty(ccList)) {
                    if (ccList.stream().anyMatch(o -> Objects.equals(o.getCcCiid(), user.getCompanyInfoId()))) {
                        return true;
                    }
                }
            }
        }

        try {
            UserServicePrx userPrx = ServantUtils.getObject(UserServicePrx.class);

            // 申请人(不打通多身份)的上级
            flag = userPrx.hasPermission(user.getContactId(), String.valueOf(applicantCid));

            if (!flag) {
                // 审批人(不打通多身份)的上级
                flag = userPrx.hasPermission(user.getContactId(), allJudgerCid);
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }

        return flag;
    }


}
